
** set path
// gl path ""

** open log
cap log close
log using "$path/table_a1.log", replace

use "$path/foreclosures_analysis.dta", clear


keep if year == 2004 | year == 2008
keep if state == "CA"
bys fips: gen n = _N
keep if n == 2

// merge on county foreclosure change variable for 2008
merge 1:1 fips year using "$path/change_merge"
drop if _merge == 2
sort fips year
by fips: gen vote_change = pres_vs - pres_vs[_n-1]

drop foreclosure_rate
rename fore_change foreclosure_rate

egen p01 = pctile(foreclosure_rate), p(50)
egen p99 = pctile(foreclosure_rate), p(99)
replace foreclosure_rate = (foreclosure_rate-p01)/(p99-p01)


reg vote_change foreclosure_rate, r
local b1 = _b[foreclosure_rate]
local se1 = _se[foreclosure_rate]
local n1 = e(N)
local nclust1 = e(N_clust)

reg vote_change foreclosure_rate [aw=pop_y], r
local b2 = _b[foreclosure_rate]
local se2 = _se[foreclosure_rate]
local n2 = e(N)
local nclust2 = e(N_clust)


use "$path/foreclosures_analysis.dta", clear

keep if year == 2004 | year == 2008
keep if state == "NV" | state == "MI" | state == "AZ" | state == "FL" | state == "RI"
bys fips: gen n = _N
keep if n == 2

// merge on county foreclosure change variable for 2008
merge 1:1 fips year using "$path/change_merge"
drop if _merge == 2
sort fips year
by fips: gen vote_change = pres_vs - pres_vs[_n-1]

drop foreclosure_rate
rename fore_change foreclosure_rate

egen p01 = pctile(foreclosure_rate), p(50)
egen p99 = pctile(foreclosure_rate), p(99)
replace foreclosure_rate = (foreclosure_rate-p01)/(p99-p01)


areg vote_change foreclosure_rate, a(state) r
local b3 = _b[foreclosure_rate]
local se3 = _se[foreclosure_rate]
local n3 = e(N)
local nclust3 = e(N_clust)

areg vote_change foreclosure_rate [aw=pop_y], a(state) r
local b4 = _b[foreclosure_rate]
local se4 = _se[foreclosure_rate]
local n4 = e(N)
local nclust4 = e(N_clust)

log close


quietly {
	cap log close
	set linesize 255
	log using "$path/foreclosure_california.tex", text replace
	
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{Effects of Housing Foreclosures on Presidential Elections, 2004--2008}"
	noisily dis "\label{tab:california}}"
	noisily dis "\begin{tabular}{lcccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{4}{c}{$ \Delta $ Dem Pres Vote (0-100)}\\[1mm]"
	noisily dis " & \multicolumn{2}{c}{California} & \multicolumn{2}{c}{Top 5} \\"
	noisily dis " & (1) & (2) & (3) & (4) \\"
	noisily dis "\midrule"
	noisily dis "$ \Delta $ Foreclosures Per 1,000 People & " %4.2f `b1' " & " %4.2f `b2' " & " %4.2f `b3' " & " %4.2f `b4' "\\"
	noisily dis " & (" %4.2f `se1' ") & (" %4.2f `se2' ") & (" %4.2f `se3' ") & (" %4.2f `se4' ") \\[2mm]"
	noisily dis " N & " %4.0fc `n1' " & " %4.0fc `n2' " & " %4.0fc `n3' " & " %4.0fc `n4'  " \\ "
	noisily dis "State Fixed Effects & -- & -- & Y & Y \\"
	noisily dis " Population Weights & N & Y & N & Y  \\"
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{5}{p{.6\textwidth}}{\footnotesize Robust standard errors clustered by county in parentheses.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	
	log off
	
}
